package com.winit.datasync.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	private static final String YMDHMS = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 将util.date 转化为sql.date
	 * 
	 * @param date
	 * @return
	 */
	public static java.sql.Date convertUtilDateToSqlDate(Date date) {

		if (null == date) {
			date = new Date();
		}

		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		java.sql.Date sqlDate = new java.sql.Date(cal.getTimeInMillis());

		return sqlDate;
	}

	/**
	 * 将sql.date 转化为util.date
	 * 
	 * @param date
	 * @return
	 */
	public static Date convertSqlDateToUtilDate(java.sql.Date date) {

		if (null == date) {
			return null;
		}

		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		Date utilDate = new Date(cal.getTimeInMillis());

		return utilDate;
	}
	
	/**
	 * 将util.date 转化为
	 * 
	 * @param date
	 * @return
	 */
	public static java.sql.Timestamp convertUtilDateToSqlTimestamp(Date date) {

		if (null == date) {
			date = new Date();
		}

		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(cal.getTimeInMillis());

		return sqlTimestamp;
	}

	/**
	 * 将sql.Timestamp 转化为util.date
	 * 
	 * @param date
	 * @return
	 */
	public static Date convertSqlTimestampToUtilDate(java.sql.Timestamp timestamp) {

		if (null == timestamp) {
			return null;
		}

		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(timestamp.getTime());
		Date utilDate = new Date(cal.getTimeInMillis());
		return utilDate;
	}


	/**
	 * date转字符串
	 * 
	 * @param date
	 * @return
	 */
	public static String convertDateToString(Date date) {
		if (null == date) {
			date = new Date();
		}

		return new SimpleDateFormat(YMDHMS).format(date);
	}

}
